using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public abstract class ConfigBase {
	
	public class ConfigDataBase{
		public int Id;
		
		public virtual void Load(LitJson.JsonData d){
			Id = int.Parse((string)d["id"]);
		}
	}
	
	/// <summary>
	/// The m config data list.
	/// </summary>
	private Dictionary<int,ConfigDataBase> mConfigDataList = new Dictionary<int, ConfigDataBase>();
	
	/// <summary>
	/// Loads the json data.
	/// </summary>
	/// <param name='data'>
	/// Data.
	/// </param>	
	public abstract void LoadJsonData(LitJson.JsonData jsonData);
	
	/// <summary>
	/// Loads the json data impl.
	/// </summary>
	/// <param name='data'>
	/// Data.
	/// </param>
	/// <typeparam name='T'>
	/// The 1st type parameter.
	/// </typeparam>
	protected void LoadJsonDataImpl<T>(LitJson.JsonData data)where T : ConfigDataBase{
		
		for(int i =0 ;i < data.Count;i++){
			LitJson.JsonData d = data[i];
			
			T dataBase = System.Activator.CreateInstance<T>();
			dataBase.Load(d);
			
			if(!mConfigDataList.ContainsKey(dataBase.Id)){
				mConfigDataList.Add(dataBase.Id,dataBase);
			}else{
				Debug.LogError(this.GetType().ToString() + " has same id:" + dataBase.Id);
			}			
		}
	}
	
	/// <summary>
	/// Gets the data base.
	/// </summary>
	/// <returns>
	/// The data base.
	/// </returns>
	/// <param name='id'>
	/// Identifier.
	/// </param>
	/// <typeparam name='T'>
	/// The 1st type parameter.
	/// </typeparam>
	public T GetDataBase<T>(int id)where T : ConfigDataBase{
	
		if(mConfigDataList.ContainsKey(id)){
			return (T)mConfigDataList[id];
		}else{
			return null;
		}	
	}
}
